package com.syt.cellphone.information.ui.main.phone.groups

import android.content.Context
import android.os.Bundle
import com.bumptech.glide.Glide
import com.syt.cellphone.information.R
import com.syt.cellphone.information.base.adapter.ItemViewHolder
import com.syt.cellphone.information.data.pojo.PhoneBase
import com.syt.cellphone.information.lib.theme.ATH
import kotlinx.android.synthetic.main.item_phone_list.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.sdk27.listeners.onLongClick

/**
 *  @author: syt
 *  创建时间: 2020/8/27 10:07
 *  @功能   列表布局
 *  @version v1.0
 */
class PhoneBaseAdapterList(context: Context, private val callBack: BasePhoneAdapter.CallBack)
    : BasePhoneAdapter(context, R.layout.item_phone_list){


    override fun convert(holder: ItemViewHolder, item: PhoneBase, payloads: MutableList<Any>) {
        val bundle = payloads.getOrNull(0) as? Bundle
        with(holder.itemView) {
            if (bundle == null) {
                ATH.applyBackgroundTint(this)
                tv_phone_item_name.text = item.baseName
                tv_phone_item_feature.text = item.baseFeature
                tv_phone_item_price.text = item.basePrice
                Glide.with(context).load(item.baseImage).into(iv_phone_item_img)
            } else {
                bundle.keySet().forEach {
                    when (it) {
                        "name" -> tv_phone_item_name.text = item.baseName
                    }
                }
            }
        }
    }

    override fun registerListener(holder: ItemViewHolder) {
        holder.itemView.apply {
            onClick {
                getItem(holder.layoutPosition)?.let {
                    callBack.openPhoneInfo(it)
                }
            }

            onLongClick {
                getItem(holder.layoutPosition)?.let {
                    callBack.openPhoneInfo(it)
                }
                true
            }
        }
    }

}